package cn.net.isir.controller.miniprogram;

import com.github.pagehelper.PageInfo;
import cn.net.isir.common.RequestObject;
import cn.net.isir.utils.RequestUtils;
import cn.net.isir.common.ResultObject;
import cn.net.isir.entity.Goods;
import cn.net.isir.service2.GoodsService;
import cn.net.isir.service2.GoodsSpecificationService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuxubo
 * @filename GoodsController.java
 * @package cn.net.isir.controller.miniprogram
 * @email lxb@isir.net.cn
 * @date 2018/11/12
 * @comment
 * @since 1.8
 */
@RestController("minipGoodsController")
@RequestMapping(value = "/minip/goods")
@Transactional
public class GoodsController {

    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodsSpecificationService goodsSpecificationService;

    @RequestMapping(method = RequestMethod.GET, value = "/hot")
    public ResultObject getHot(RequestObject obj, HttpServletRequest request, HttpServletResponse response) {
        //声明返回值
        ResultObject ro;
        //声明参数解码后的储存变量
        Map<String, Object> reqParamMap = new HashMap<>();
        //需要检查的字段名
        String[] columns = new String[]{"shopid"};
        //检查失败则返回缺失的字段提示u
        if ((ro = RequestUtils.parametersCheck(obj, reqParamMap, columns)).getResult() != 200) {
            return ro;
        }
        String shopid = String.valueOf(reqParamMap.get("shopid"));
        Goods goods = new Goods();
        goods.setShopId(shopid);
        goods.setIsHotSeal(1);
        goods.setSaleStatus("onsale");
        List<Goods> goodsList = goodsService.selectList(goods);
        ro.setResult(200);
        ro.setMessage("success");
        ro.setObject(goodsList);
        return ro;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public ResultObject getGoodsByCategoryId(RequestObject obj, HttpServletRequest request, HttpServletResponse response) {
        //声明返回值
        ResultObject ro;
        //声明参数解码后的储存变量
        Map<String, Object> reqParamMap = new HashMap<>();
        //需要检查的字段名
        String[] columns = new String[]{"shopid", "categoryid", "pagenum", "pagesize"};
        //检查失败则返回缺失的字段提示
        if ((ro = RequestUtils.parametersCheck(obj, reqParamMap, columns)).getResult() != 200) {
            return ro;
        }
        String shopid = String.valueOf(reqParamMap.get("shopid"));
        String categoryid = String.valueOf(reqParamMap.get("categoryid"));
        String pagenumStr = String.valueOf(reqParamMap.get("pagenum"));
        String pagesizeStr = String.valueOf(reqParamMap.get("pagesize"));

        Integer pagenum;
        Integer pagesize;
        try {
            pagenum = Integer.parseInt(pagenumStr);
            pagesize = Integer.parseInt(pagesizeStr);
        } catch (Exception ex) {
            ex.printStackTrace();
            ro.setResult(500);
            ro.setMessage("分页参数错误");
            return ro;
        }

        Goods goods = new Goods();
        goods.setShopId(shopid);
        goods.setCategoryId(categoryid);
        goods.setSaleStatus("onsale");
        PageInfo<Goods> goodsList = goodsService.selectPageList(goods, pagenum, pagesize);
        ro.setResult(200);
        ro.setMessage("success");
        ro.setObject(goodsList);
        return ro;
    }
}
